package com.ruoyi.system.controller;

import com.ruoyi.system.domain.Department;
import com.ruoyi.system.domain.vo.PageResult;
import com.ruoyi.system.domain.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.service.IDoctorScheduleService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.system.domain.vo.DoctorScheduleVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 医生排班Controller
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/schedule")
@Validated
public class DoctorScheduleController extends BaseController {
    @Autowired
    private IDoctorScheduleService doctorScheduleService;

    /**
     * 分页条件查询医生排班信息
     * 
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param departmentId 科室ID
     * @param patientName 就诊人姓名
     * @param weekStart 周开始日期
     * @param weekEnd 周结束日期
     * @return 分页结果
     */
    @GetMapping("/list")
    public PageResult list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer departmentId,
            @RequestParam(required = false) String patientName,
            @RequestParam(required = true) String weekStart,
            @RequestParam(required = true) String weekEnd
    ) {
        // 参数校验
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 || pageSize > 100) {
            pageSize = 10;
        }
        return doctorScheduleService.selectDoctorScheduleByWeek(pageNum, pageSize, departmentId, patientName, weekStart, weekEnd);
    }

    /**
     * 新增医生排班信息
     */
    @PostMapping
    public Result add(@RequestBody DoctorScheduleVO doctorScheduleVO) {
        return doctorScheduleService.insertDoctorSchedule(doctorScheduleVO);
    }

    /**
     * 修改医生排班信息
     */
    @PutMapping
    public Result edit(@RequestBody DoctorScheduleVO doctorScheduleVO) {
        return doctorScheduleService.updateDoctorSchedule(doctorScheduleVO);
    }

    /**
     * 删除医生排班信息
     */
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id) {
        return doctorScheduleService.deleteDoctorScheduleById(id);
    }

    /**
     * 查询单条医生排班信息
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id) {
        return doctorScheduleService.selectDoctorScheduleById(id);
    }

    /**
     * 医生排班详情（单天上午/下午）
     */
    @GetMapping("/detail")
    public Result getDoctorScheduleDetail(
            @RequestParam Long doctorId,
            @RequestParam String date,      // yyyy-MM-dd
            @RequestParam String period     // "morning"/"afternoon" 或 "上午"/"下午"
    ) {
        return doctorScheduleService.getDoctorScheduleDetail(doctorId, date, period);
    }

    /**
     * 查询科室全部
     */
    @GetMapping("/department/list")
    public Result getDepartmentList() {
        return doctorScheduleService.getDepartmentList();
    }
}
